package com.company.texturewithlistview;

import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.khronos.opengles.GL10;

import com.company.texturewithlistview.OpenGLRenderer.TextureLoader;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.opengl.GLES10;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private GLSurfaceView	mGlSurfaceView;
	private ListView		mListView;
	private Bitmap			mBitmap;
	private TextureLoader	mteTextureLoader;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mListView	= (ListView)findViewById(R.id.list_view);
		mListView.setScrollingCacheEnabled( false );
		mListView.setAdapter( new MyListAdapter() );
		
		mteTextureLoader = new MyTextureLoader();
		
		mGlSurfaceView = (GLSurfaceView) findViewById(R.id.glsurface_view);
//		mGlSurfaceView.setEGLConfigChooser(false);
//		mGlSurfaceView.getHolder().setFormat(PixelFormat.RGB_565);
		mGlSurfaceView.setRenderer(new OpenGLRenderer(this, mteTextureLoader));
		
		
    }
    
    public void CopyDraw( Bitmap bitmap ) { 	
    	mBitmap = bitmap;  
//        InputStream is = getResources().openRawResource(R.drawable.nehe);
//        mBitmap = BitmapFactory.decodeStream(is);
    	
	}
    
    private class MyListAdapter extends BaseAdapter {
		//-------------------------------------------------------------------------------
		public int getCount() { return 20; }
		public long getItemId(int position) { return position; }
		public Object getItem(int position) { return position; }
		//-------------------------------------------------------------------------------
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView item = (TextView)convertView;
			if( item == null ) item = new TextView(MainActivity.this );
			item.setText( String.format( "%03d: ABCDEFGHIJKLMNOPQRTU", position ) );
			item.setPadding( 15, 15, 15, 15 );
			return item;
		}
	}
    
    private class MyTextureLoader implements TextureLoader {
		@Override
		public void loadTexture(GL10 gl) {
			GLUtils.texImage2D(GLES10.GL_TEXTURE_2D, 0, mBitmap, 0);
			mBitmap.recycle();
		}
	}
}